function clearMessages() {
  jQuery('#messages').html('');
  jQuery('#messages').removeClass("messages");
  jQuery('#messages').removeClass("error");
  jQuery('#messages').removeClass("warning");
  jQuery('#messages').removeClass("status");
}

function displayMessage(type, msg) {
  var elem_msgs = jQuery('#messages');
  elem_msgs.html(msg);
  elem_msgs.addClass("messages");
  switch (type) {
    case 'error':
      elem_msgs.addClass("error");
    break;
    case 'warning':
      elem_msgs.addClass("warning");
    break;
    case 'status':
      elem_msgs.addClass("status");
    break;
  }
  // Check is messages div is not visible currently. Only then scroll it in position
  if (jQuery(window).scrollTop() > elem_msgs.offset().top || ((elem_msgs.offset().top + elem_msgs.height()) > jQuery(window).height())) {
    var top = elem_msgs.offset().top;
    jQuery('html,body').animate({scrollTop: top}, 500);
  }
  
  return false;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function getDDSelectedText(elem_id) {
  return jQuery('#' + elem_id).selectedOptions()[0].text;
}

function getDDSelectedValue(elem_id) {
  return jQuery('#' + elem_id).selectedOptions().val();
}

function getDDSelectedValues(elem_id) {
  var options = jQuery('#' + elem_id).selectedOptions();
  var len = options.length;
  var vals = [];
  for (var i = 0; i < len; i++) {
    vals.push(options[i].value);
  }
  return vals;
}

function getTextboxVal(elem_id) {
  return jQuery('#' + elem_id).val();
}

function isCheckboxChecked(elem_id) {
  //if (getTextboxVal('#' + elem_id + ':checked') !== null) {
  if (jQuery('#' + elem_id + ':checked').length > 0) {
    return true;
  } else {
    return false;
  }
}

function getRadioCheckedValue(elem_name) {
  return jQuery("input[name='" + elem_name + "']:checked").val();
}

function disableElem(elem, flag) {
  if (flag) {
    elem.attr('disabled', 'disabled');
  }
  else {
    elem.removeAttr('disabled');
  }
}

function reloadCaptcha(img_elem_id) {
  jQuery('#' + img_elem_id).attr('src', Drupal.settings.basePath + 'observation/data/{"action":"getCaptchaImage"}?' + new Date().getTime());
  return false;
}

function selectOptionByText(elem, txt) { // elem is the select box returned by getElementById. Hence jQuery(elem)[0].
  var opts = elem.options;
  var len = opts.length;

  for (var i = 0; i < len; i++) {
    var opt = opts[i];
    if (opt.text == txt) {
      opt.selected = true;
      return i;
    }
  }
}

function in_array (needle, haystack, argStrict) {
  // Checks if the given value exists in the array
  var key = '', strict = !!argStrict;
  if (strict) {
    for (key in haystack) {
      if (haystack[key] === needle) {
        return true;            }
    }
  } else {
    for (key in haystack) {
      if (haystack[key] == needle) {
        return true;
      }
    }
  }
   return false;
}
